package com.lxc.infrastructure.persistent.repository;

import com.lxc.domain.manage.model.entity.*;
import com.lxc.domain.manage.repository.IConfigManageRepository;
import com.lxc.infrastructure.persistent.dao.*;
import com.lxc.infrastructure.persistent.po.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxc18
 * @date 2024/12/14 20:53
 * @description ConfigManageRepository
 */
@Repository
public class ConfigManageRepository implements IConfigManageRepository {
    @Resource
    private IGatewayServerDao gatewayServerDao;
    @Resource
    private IGatewayServerDetailDao gatewayServerDetailDao;
    @Resource
    IGatewayDistributionDao gatewayDistributionDao;
    @Resource
    IApplicationSystemDao applicationSystemDao;
    @Resource
    IApplicationInterfaceDao applicationInterfaceDao;
    @Resource
    IApplicationInterfaceMethodDao applicationInterfaceMethodDao;

    @Override
    public List<GatewayServerEntity> queryGatewayServerList() {
        List<GatewayServer> gatewayServers = gatewayServerDao.queryGatewayServerList();
        List<GatewayServerEntity> gatewayServerVOList = new ArrayList<>(gatewayServers.size());
        for (GatewayServer gatewayServer : gatewayServers) {
            // 可以按照 IDEA 插件 vo2dto 方便转换
            GatewayServerEntity gatewayServerEntity = new GatewayServerEntity();
            gatewayServerEntity.setGroupId(gatewayServer.getGroupId());
            gatewayServerEntity.setGroupName(gatewayServer.getGroupName());
            gatewayServerVOList.add(gatewayServerEntity);
        }
        return gatewayServerVOList;

    }

    @Override
    public boolean registerGatewayServerNode(String groupId, String gatewayId, String gatewayName, String gatewayAddress, Integer available) {
        GatewayServerDetail gatewayServerDetail = new GatewayServerDetail();
        gatewayServerDetail.setGroupId(groupId);
        gatewayServerDetail.setGatewayId(gatewayId);
        gatewayServerDetail.setGatewayName(gatewayName);
        gatewayServerDetail.setGatewayAddress(gatewayAddress);
        gatewayServerDetail.setStatus(available);
        gatewayServerDetailDao.insert(gatewayServerDetail);
        return true;

    }

    @Override
    public GatewayServerDetailEntity queryGatewayServerDetail(String gatewayId, String gatewayAddress) {

        GatewayServerDetail gatewayServerDetail = gatewayServerDetailDao.queryGatewayServerDetail(gatewayId,gatewayAddress);
        if (null == gatewayServerDetail) return null;
        // 可以按照 IDEA 插件 vo2dto 方便转换
        GatewayServerDetailEntity gatewayServerEntity = new GatewayServerDetailEntity();
        gatewayServerEntity.setGroupId(gatewayServerDetail.getGroupId());
        gatewayServerEntity.setGatewayId(gatewayServerDetail.getGatewayId());
        gatewayServerEntity.setGatewayName(gatewayServerDetail.getGatewayName());
        gatewayServerEntity.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
        gatewayServerEntity.setStatus(gatewayServerDetail.getStatus());

        return gatewayServerEntity;

    }

    @Override
    public boolean updateGatewayStatus(String gatewayId, String gatewayAddress, Integer available) {
        GatewayServerDetail gatewayServerDetail = new GatewayServerDetail();
        gatewayServerDetail.setGatewayId(gatewayId);
        gatewayServerDetail.setGatewayAddress(gatewayAddress);
        gatewayServerDetail.setStatus(available);
        return gatewayServerDetailDao.updateGatewayStatus(gatewayServerDetail);

    }

    @Override
    public List<ApplicationInterfaceEntity> getApplicationInterfaceBySystemId(String systemId) {
        List<ApplicationInterface> applicationInterfaceBySystemId = applicationInterfaceDao.getApplicationInterfaceBySystemId(systemId);
        ArrayList<ApplicationInterfaceEntity> applicationInterfaceEntities = new ArrayList<>(applicationInterfaceBySystemId.size());
        for(ApplicationInterface applicationInterface : applicationInterfaceBySystemId){
            ApplicationInterfaceEntity applicationInterfaceEntity = new ApplicationInterfaceEntity();
            applicationInterfaceEntity.setSystemId(applicationInterface.getSystemId());
            applicationInterfaceEntity.setInterfaceId(applicationInterface.getInterfaceId());
            applicationInterfaceEntity.setInterfaceName(applicationInterface.getInterfaceName());
            applicationInterfaceEntity.setInterfaceVersion(applicationInterface.getInterfaceVersion());
            applicationInterfaceEntities.add(applicationInterfaceEntity);
        }
        return applicationInterfaceEntities;
    }

    @Override
    public ApplicationSystemEntity getApplicationSystemBySystemId(String systemId) {
        ApplicationSystem applicationSystemBySystemId = applicationSystemDao.getApplicationSystemBySystemId(systemId);
        if(applicationSystemBySystemId == null) return null;
        ApplicationSystemEntity applicationSystemEntity = new ApplicationSystemEntity();
        applicationSystemEntity.setSystemId(applicationSystemBySystemId.getSystemId());
        applicationSystemEntity.setSystemName(applicationSystemBySystemId.getSystemName());
        applicationSystemEntity.setSystemType(applicationSystemBySystemId.getSystemType());
        applicationSystemEntity.setSystemRegistry(applicationSystemBySystemId.getSystemRegistry());

        return applicationSystemEntity;
    }

    @Override
    public List<ApplicationInterfaceMethodEntity> getMethodsByInterfaceId(String interfaceId) {
        List<ApplicationInterfaceMethod> methods = applicationInterfaceMethodDao.getMethodsByInterfaceId(interfaceId);
        ArrayList<ApplicationInterfaceMethodEntity> res = new ArrayList<>(methods.size());
        for (ApplicationInterfaceMethod method :methods){
            ApplicationInterfaceMethodEntity applicationInterfaceMethodEntity = new ApplicationInterfaceMethodEntity();
            applicationInterfaceMethodEntity.setSystemId(method.getSystemId());
            applicationInterfaceMethodEntity.setInterfaceId(method.getInterfaceId());
            applicationInterfaceMethodEntity.setMethodId(method.getMethodId());
            applicationInterfaceMethodEntity.setMethodName(method.getMethodName());
            applicationInterfaceMethodEntity.setParameterType(method.getParameterType());
            applicationInterfaceMethodEntity.setUri(method.getUri());
            applicationInterfaceMethodEntity.setHttpCommandType(method.getHttpCommandType());
            applicationInterfaceMethodEntity.setAuth(method.getAuth());
            res.add(applicationInterfaceMethodEntity);
        }
        return res;
    }

    @Override
    public List<String> getGatewayDistributionListByGatewayId(String gatewayId) {
        return gatewayDistributionDao.getGatewayDistributionListByGatewayId(gatewayId);
    }

    @Override
    public List<GatewayServerDetailEntity> queryGatewayServerDetailList() {
        List<GatewayServerDetail> gatewayServerDetails = gatewayServerDetailDao.queryGatewayServerDetailList();
        List<GatewayServerDetailEntity> gatewayServerDetailEntities = new ArrayList<>(gatewayServerDetails.size());
        for(GatewayServerDetail gatewayServerDetail : gatewayServerDetails){
            GatewayServerDetailEntity gatewayServerDetailEntity = new GatewayServerDetailEntity();
            gatewayServerDetailEntity.setGroupId(gatewayServerDetail.getGroupId());
            gatewayServerDetailEntity.setGatewayId(gatewayServerDetail.getGatewayId());
            gatewayServerDetailEntity.setGatewayName(gatewayServerDetail.getGatewayName());
            gatewayServerDetailEntity.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
            gatewayServerDetailEntity.setStatus(gatewayServerDetail.getStatus());
            gatewayServerDetailEntities.add(gatewayServerDetailEntity);

        }
        return gatewayServerDetailEntities;
    }

}
